/**
 * 
 */
package org.carabiner.infinitest.swingui;

import java.awt.Color;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.carabiner.infinitest.InfinitestSettings;
import org.carabiner.infinitest.InfinitestUtils;
import org.carabiner.infinitest.TestEvent;

/**
 * 
 * @author <a href="mailto:benrady@gmail.com"Ben Rady</a>
 */
class ResultTreeNode extends TestTreeNode
{
  static final Color ERROR_COLOR = new Color(0xb22222);

  static final Icon errorIcon = new ImageIcon(ResultTreeNode.class.getResource("error.png"));
  static Icon failureIcon = new ImageIcon(ResultTreeNode.class.getResource("failure.png"));

  public ResultTreeNode(TestEvent testEvent)
  {
    super(testEvent);
    setAllowsChildren(false);
  }

  public boolean isNodeForEvent(TestEvent e)
  {
    return super.isNodeForEvent(e) && e.getTestMethod().equals(getTestEvent().getTestMethod());
  }

  public String getToolTip()
  {
    if (getTestEvent().getError() == null)
      return null;
    return InfinitestUtils.stackTraceToHTML(getTestEvent().getError().getStackTrace());
  }

  public String toString()
  {
    String baseText = getTestEvent().getTestMethod().toString() + ": ";
    if (getTestEvent().getError() != null && !getTestEvent().isFailure())
    {
      return baseText + getTestEvent().getError().getClass().getName() + "(" + getTestEvent().getMessage() + ")";
    }
    if (getTestEvent().getMessage() == null)
    {
      return baseText + getTestEvent().getError().getClass().getSimpleName();
    }
    return baseText + getTestEvent().getMessage();
  }

  @Override
  public Icon getIcon()
  {
    if (getTestEvent().isFailure())
      return failureIcon;
    return errorIcon;
  }

  @Override
  public Color getColor()
  {
    if (getTestEvent().isFailure())
      return InfinitestSettings.FAILURE_COLOR;
    return ERROR_COLOR;
  }

}